# a simple C only test case
cmake_minimum_required (VERSION 2.6)
project (ReturnTest)

function (FAILED testname)
  message (SEND_ERROR "${testname} failed ${ARGN}")
endfunction (FAILED)

function (PASS testname)
  message ("${testname} passed ${ARGN}")
endfunction (PASS)

# test simple return
function (simple)
  set(simpleResult 1 PARENT_SCOPE)
  return()
  set(simpleResult 0 PARENT_SCOPE)
endfunction (simple)
simple()
if ("${simpleResult}")
  pass ("simple")
else ("${simpleResult}")
  failed ("simple got: ${simpleResult}")
endif ("${simpleResult}")

#test return in an if statement
set (simple2IF 1)
function (simple2)
  set(simple2Result 1 PARENT_SCOPE)
  if (simple2IF)
    return()
  endif (simple2IF)
  set(simple2Result 0 PARENT_SCOPE)
endfunction (simple2)
simple2()
if ("${simple2Result}")
  pass ("simple2")
else ("${simple2Result}")
  failed ("simple2 got: ${simple2Result}")
endif ("${simple2Result}")

#test return in a foreach loop
function (looptest)
  foreach (iter RANGE 1 5)
    set (looptestResult "${iter}" PARENT_SCOPE)
    if ("${iter}" EQUAL 3)
      return ()
    endif ("${iter}" EQUAL 3)
  endforeach (iter)
endfunction (looptest)
looptest()
if ("${looptestResult}" EQUAL 3)
  pass ("looptest")
else ("${looptestResult}" EQUAL 3)
  failed ("looptest got: ${looptestResult}")
endif ("${looptestResult}" EQUAL 3)

#test return in a while loop
function (whiletest)
  set (iter "a")
  while(NOT "${iter}" STREQUAL "aaaaa")
    set (whiletestResult "${iter}" PARENT_SCOPE)
    if ("${iter}" STREQUAL "aaa")
      return ()
    endif ("${iter}" STREQUAL "aaa")
    set (iter "${iter}a")
  endwhile(NOT "${iter}" STREQUAL "aaaaa")
endfunction (whiletest)
whiletest()
if ("${whiletestResult}" STREQUAL "aaa")
  pass ("whiletest")
else ("${whiletestResult}" STREQUAL "aaa")
  failed ("whiletest got: ${whiletestResult}")
endif ("${whiletestResult}" STREQUAL "aaa")

# check subdir return
add_subdirectory(subdir)
get_directory_property(subdirResult DIRECTORY subdir DEFINITION subdirreturn)
if ("${subdirResult}" EQUAL 1)
  pass ("subdir")
else ("${subdirResult}" EQUAL 1)
  failed ("subdir got: ${subdirResult}")
endif ("${subdirResult}" EQUAL 1)

# check return from a file
include(include_return.cmake)
if ("${include_returnResult}" EQUAL 1)
  pass ("include_return")
else ("${include_returnResult}" EQUAL 1)
  failed ("include_return got: ${include_returnResult}")
endif ("${include_returnResult}" EQUAL 1)

# check return from within a macro
macro (mymacro)
  set (foo 1)
  if (foo)
    return()
  endif (foo)
endmacro(mymacro)

# test simple return
function (simple3)
  set (bar 0)
  set(simple3Result 1 PARENT_SCOPE)
  if (bar)
  else (bar)
    mymacro()
  endif(bar)
  set(simple3Result 0 PARENT_SCOPE)
endfunction (simple3)
simple3()
if ("${simple3Result}")
  pass ("macrotest")
else ("${simple3Result}")
  failed ("macrotest got: ${simple3Result}")
endif ("${simple3Result}")


# test break command now in a foreach
foreach (iter RANGE 1 5)
  set (break1 "${iter}")
  if ("${iter}" EQUAL 3)
    break ()
  endif ("${iter}" EQUAL 3)
endforeach (iter)
if ("${break1}" EQUAL 3)
  pass ("break in foreach")
else ("${break1}" EQUAL 3)
  failed ("break in foreach got: ${break1}")
endif ("${break1}" EQUAL 3)

# test break in a while loop
set (iter "a")
while(NOT "${iter}" STREQUAL "aaaaa")
  if ("${iter}" STREQUAL "aaa")
    break ()
  endif ("${iter}" STREQUAL "aaa")
  set (iter "${iter}a")
endwhile(NOT "${iter}" STREQUAL "aaaaa")
if ("${iter}" STREQUAL "aaa")
  pass ("break in a while")
else ("${iter}" STREQUAL "aaa")
  failed ("break in a whi;e got: ${whiletestResult}")
endif ("${iter}" STREQUAL "aaa")


add_executable (ReturnTest returnTest.c)
